1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.math;
18
19 import static com.google.common.math.MathBenchmarking.ARRAY_MASK;
20 import static com.google.common.math.MathBenchmarking.ARRAY_SIZE;
21 import static com.google.common.math.MathBenchmarking.randomNonZeroBigInteger;
22 import static com.google.common.math.MathBenchmarking.randomPositiveBigInteger;
23
24 import com.google.caliper.BeforeExperiment;
25 import com.google.caliper.Benchmark;
26 import com.google.caliper.Param;
27 import com.google.common.math.BigIntegerMath;
28
29 import java.math.BigInteger;
30 import java.math.RoundingMode;
31
32
33
34
35
36
37 public class BigIntegerMathRoundingBenchmark {
38 private static final BigInteger[] nonzero1 = new BigInteger[ARRAY_SIZE];
39 private static final BigInteger[] nonzero2 = new BigInteger[ARRAY_SIZE];
40 private static final BigInteger[] positive = new BigInteger[ARRAY_SIZE];
41
42 @Param({"DOWN", "UP", "FLOOR", "CEILING", "HALF_EVEN", "HALF_UP", "HALF_DOWN"})
43 RoundingMode mode;
44
45 @BeforeExperiment
46 void setUp() {
47 for (int i = 0; i < ARRAY_SIZE; i++) {
48 positive[i] = randomPositiveBigInteger(1024);
49 nonzero1[i] = randomNonZeroBigInteger(1024);
50 nonzero2[i] = randomNonZeroBigInteger(1024);
51 }
52 }
53
54 @Benchmark int log2(int reps) {
55 int tmp = 0;
56 for (int i = 0; i < reps; i++) {
57 int j = i & ARRAY_MASK;
58 tmp += BigIntegerMath.log2(positive[j], mode);
59 }
60 return tmp;
61 }
62
63 @Benchmark int log10(int reps) {
64 int tmp = 0;
65 for (int i = 0; i < reps; i++) {
66 int j = i & ARRAY_MASK;
67 tmp += BigIntegerMath.log10(positive[j], mode);
68 }
69 return tmp;
70 }
71
72 @Benchmark int sqrt(int reps) {
73 int tmp = 0;
74 for (int i = 0; i < reps; i++) {
75 int j = i & ARRAY_MASK;
76 tmp += BigIntegerMath.sqrt(positive[j], mode).intValue();
77 }
78 return tmp;
79 }
80
81 @Benchmark int divide(int reps) {
82 int tmp = 0;
83 for (int i = 0; i < reps; i++) {
84 int j = i & ARRAY_MASK;
85 tmp += BigIntegerMath.divide(nonzero1[j], nonzero2[j], mode).intValue();
86 }
87 return tmp;
88 }
89 }